package com.domor.web.controller.monitor;

import com.domor.common.annotation.Log;
import com.domor.common.core.controller.BaseController;
import com.domor.common.core.domain.AjaxResult;
import com.domor.common.core.page.TableDataInfo;
import com.domor.common.utils.poi.ExcelUtil;
import com.domor.framework.shiro.service.SysPasswordService;
import com.domor.system.domain.SysLoginLog;
import com.domor.system.service.SysLoginLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <h3>系统访问记录</h3>
 * @author liyuyang
 * @date 2020/5/6
 **/
@Controller
@RequestMapping("/monitor/loginLog")
public class SysLoginLogController extends BaseController {

    private String prefix = "monitor/loginLog";

    @Autowired
    private SysLoginLogService loginLogService;

    @Autowired
    private SysPasswordService passwordService;

    @GetMapping()
    public String loginLog() {
        return prefix + "/index";
    }

    @RequiresPermissions("monitor:loginLog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysLoginLog loginLog) {
        setOrderByAndPage();
        List<SysLoginLog> list = loginLogService.selectLoginLogList(loginLog);
        return getDataTable(list);
    }

    @Log(title = "登陆日志")
    @RequiresPermissions("monitor:loginLog:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysLoginLog loginLog) {
        List<SysLoginLog> list = loginLogService.selectLoginLogList(loginLog);
        ExcelUtil<SysLoginLog> util = new ExcelUtil<SysLoginLog>(SysLoginLog.class);
        return util.exportExcel(list, "登陆日志");
    }

    @RequiresPermissions("monitor:loginLog:remove")
    @Log(title = "登陆日志")
    @PostMapping("/delete")
    @ResponseBody
    public AjaxResult delete(String ids) {
        return toAjax(loginLogService.deleteLoginLogByIds(ids));
    }

    @RequiresPermissions("monitor:loginLog:remove")
    @Log(title = "登陆日志")
    @PostMapping("/clean")
    @ResponseBody
    public AjaxResult clean() {
        loginLogService.cleanLoginLog();
        return success();
    }

    @RequiresPermissions("monitor:loginLog:unlock")
    @Log(title = "账户解锁")
    @PostMapping("/unlock")
    @ResponseBody
    public AjaxResult unlock(String loginName) {
        passwordService.unlock(loginName);
        return success();
    }

}
